<?php
namespace app\index\controller;
use think\Db;
class News
{
    // 这是查找指定类型的新闻
	public function newsList(){
        $params = $_GET;
		$data['data'] = Db::table('website_new')->where('new_type',$params['type'])->limit($_GET['pageSize']*$_GET['currentPage'],$_GET['pageSize'])->select();
		$data['count'] = Db::table('website_new')->where('new_type',$params['type'])->count();
		echo json_encode($data);
	}
	// 这是查找指定Id的新闻
	public function newsInfo(){
        $params = $_GET;
		$data['data'] = Db::table('website_new')->where('id',$params['id'])->find();
		echo json_encode($data);
	}
	/* 
	* 参数解析
	* 参数1：newId：这个是新闻的ID
	* 参数2：newTitle：这个是新闻的标题
	* 参数3：newType：这个是新闻的类型
	* 参数4：pageSize：这个是每次查询的数据条数
	* 参数5：currentPage：这是是查找第几页数据
	*/
	public function findNews(){
		$params = $_GET;
		$flag = true;
		$map;
		if($params['newId'] != null || $params['newId'] != ""){
			$flag = "false";
			$map['website_new.id'] = $params['newId'];		
		}
		if($params['newTitle'] != null || $params['newTitle'] != ""){
			$flag = "false";
			$map['website_new.title'] =  array('like','%'.$params['newTitle'].'%'); 	
		}
		if($params['newType'] != null || $params['newType'] != ""){
			$flag = "false";
			$map['website_new.new_type'] = $params['newType'];
		}
		if($flag == "true"){
			$data['data'] = Db::table('website_new')->alias('t1')->join('website_menu t2','t1.new_type = t2.id')->field('t1.id,t1.title,t1.sub_title,t1.update_time,t2.name as name')->order('t1.update_time desc')->limit($params['pageSize']*$params['currentPage'],$params['pageSize'])->select();
			$data['count'] = Db::table('website_new')->count();
		}else{
			$data['data'] = Db::table('website_new')->alias('t1')->join('website_menu t2','t1.new_type = t2.id')->field('t1.id,t1.title,t1.sub_title,t1.update_time,t2.name as name')->where($map)->order('t1.update_time desc')->limit($params['pageSize']*$params['currentPage'],$params['pageSize'])->select();
			$data['count'] = Db::table('website_new')->where($map)->count();
		}
		echo json_encode($data);
	}

	/*
	* 这是删除新闻的方法 
	* 参数解析
	* 参数1：ids：这个是要删除的新闻ID集合格式是（1,2,3）
	*/
	public function deleteNews(){
		$params = $_GET;
		// 这里是把前端的参数转换成一个数组进行操作
		$arr = explode(',',$params['ids']);
		$data['data'] = Db::table('website_new')->delete($arr);
		$data['message'] = "删除成功！";
		$data['status'] = "200";
		echo json_encode($data);
	}

	// 这里是图片上传的方法
	public function upload(){
		// 移动到框架应用根目录/public/uploads/ 目录下
		$file = request()->file('file');
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	        if($info){
	            // 成功上传后 获取上传信息
	            // 输出 jpg
	            //echo $info->getExtension();
	            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
	           // echo $info->getSaveName();
	            $image = $info->getSaveName();
	            // 输出 42a79759f284b767dfcb2a0197904287.jpg
				$data['file_path']='http://'.$_SERVER['HTTP_HOST'].'/public/uploads/'.$image;
				$data['msg']="成功";
				$data['success']=true;
				//echo $info->getFilename(); 
				echo json_encode($data);
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
		} 
	}

	// 这里保存新闻的方法
	public function saveNews(){
		$params = $_GET;
		$data = [
			'title' => $params["newTitle"],
			'sub_title'=>$params["subTitle"],
			'content'=>$params["content"],
			'img_url'=>$params["imageUrl"],
			'new_type'=>$params["newType"],
			'updater'=>'1',
			'update_time'=>date("Y/m/d H:m:s")
	   	];
	   	if($params['newId']=="" || $params['newId']==null){
			$result["data"] = Db::table('website_new')->insert($data);
		}else{
			$result["data"] = Db::table('website_new')->where('id', $params['newId'])->update($data);
		}
	   
	    $result["status"] = "200";
	    echo  json_encode($result);
	}
}
